from common.enum.data_map_key import DataDictionaryKey
from common.enum.form_items_key import FormItemsKey
from service.setting_service import DataDictionaryService
from .das.stock_monitor_das import StockDas, AntMonitorStock
from .das.stock_day_das import StockDateDayDas
from .das.tushare_pool import getDayStock, getRealTime
from datetime import datetime, timedelta
from .das.sina_das import get_stock_info
from dto.stock_info_monitor import AntMonitorStockInfo


def getAllStockList():
    stock_das = StockDas()
    stockList = stock_das.get_stocks()
    return stockList


def getAllStockListSample():
    stock_das = StockDas()
    stockList = stock_das.get_stocks()
    stockInfo = [f"{stock.stock_name} & {stock.stock_code}" for stock in stockList]
    return stockInfo


def saveMonitorStock(stockInfo: AntMonitorStockInfo):
    # 获取今天的日期
    today = datetime.today()
    # 获取一年前的日期
    dataMap = DataDictionaryService()
    result = dataMap.get_item_by_enum(FormItemsKey.TUSHARE_INIT_DAY)
    ten_year_ago = today - timedelta(days=10)

    # 保存 Stock 数据
    stock_das = StockDas()
    stock = stock_das.get_stocks_byCode(stockInfo.stock_code)
    if stock:
        print(f"数据已存在,无需添加")
        return

    # 聚合出股票名称
    stockDetail = get_stock_info(stockInfo.stock_code)
    stockInfo.stock_name = stockDetail.name

    stock_das.add_stock(stockInfo)

    #
    today_str = today.strftime('%Y%m%d')
    one_year_ago_str = ten_year_ago.strftime('%Y%m%d')

    df = getDayStock(stockInfo.stock_code, one_year_ago_str, today_str)
    print(f"开始保存拉取到到当个股票的数据")
    StockDateDayDas.add_stock(df)


def updateMonitorStock(stockInfo: AntMonitorStockInfo):
    stock_das = StockDas()
    stock_das.update_stock_bycode(stockInfo)


def deleteOneStock(stockCode):
    # 删除历史的 StockMonitor 的信息
    stock_das = StockDas()
    stock = stock_das.delete_bycode(stockCode)

    # 删除历史的 每日数据
    StockDateDayDas.delete_bycode(stockCode)


def getByStockCode(stockCode):
    stock_das = StockDas()
    stock_record = stock_das.get_stocks_byCode(stockCode)
    return AntMonitorStockInfo(
        stock_code=stock_record.stock_code,
        stock_name=stock_record.stock_name,
        stock_focus_level=stock_record.stock_focus_level,
        stock_risk_level=stock_record.stock_risk_level,
        stock_current_number=stock_record.stock_current_number,
        stock_max_number=stock_record.stock_max_number,
        monitor_origin_price=stock_record.monitor_origin_price,
        monitor_current_price=stock_record.monitor_current_price,
        monitor_income_ratio=stock_record.monitor_income_ratio,
        monitor_income_money=stock_record.monitor_income_money,
        monitor_target_ratio=stock_record.monitor_target_ratio,
        monitor_stop_ratio=stock_record.monitor_stop_ratio,
        stock_status=stock_record.stock_status,
        stock_tag=stock_record.stock_tag,
        next_advice=stock_record.stock_next_advice,
        month_up_day=stock_record.month_up_day,
        week_up_day=stock_record.week_up_day,
        other_info=stock_record.other_info
    )


def getRealTimeStock(stockCode):
    print(f"当前处理的股票{stockCode}")
    df = getRealTime(stockCode)
    print(f"获取到股票代码:{df}")
    result = [["stock_price", "type", "time"]]
    if df is not None:
        for index, row in df.iterrows():
            result.append([float(row['PRICE']), "real", int(row['TIME'].replace(':', ''))])

            
    return result
